﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using jianvgroup.com.Models.CompanyNewsModels;
using data.jianvgroup.com;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using jianvgroup.com.Models;
using data.jianvgroup.com.Entity;
using Microsoft.Extensions.Logging;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace jianvgroup.com.Controllers
{
    /// <summary>
    /// 公司动态
    /// </summary>
    public class CompanyNewsController : Controller
    {

        private readonly JvgroupContext _db;
        private readonly ILogger<CompanyNewsController> _log;
        public CompanyNewsController(JvgroupContext db,
            ILogger<CompanyNewsController> log)
        {
            _db = db;
            _log = log;
        }

        // GET: /<controller>/
        public async Task<IActionResult> Index()
        {
            var newList = new List<NewsModel>();
            var news = await _db.news.ToListAsync();
            if (news != null && news.Any())
            {
                foreach (var item in news)
                {
                    newList.Add(new NewsModel()
                    {
                        Content = item.Content,
                        Status = item.Status,
                        Remark = item.Remark,
                        TitleName = item.TitleName,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Creater = User.Identity.Name,
                        Modofier = User.Identity.Name,
                        SysId = item.SysId
                    });
                }
            }
            return View(newList);
        }

        [HttpGet]
        public async Task<IActionResult> Create(NewsModel newModel,int id)
        {
            try
            {
                if (id > 0)
                {
                    var news = await _db.news.FirstOrDefaultAsync(x => x.SysId == id);
                    if (news != null)
                    {
                        newModel.SysId = news.SysId;
                        newModel.Content = news.Content;
                        newModel.Remark = news.Remark;
                        newModel.TitleName = news.TitleName;
                        newModel.Status = news.Status;
                    }
                }
                ViewBag.Status = GetStatus(newModel.SysId.ToString());
            }
            catch (Exception ex)
            {
                _log.LogWarning("编辑新闻操作异常", ex.ToString());
            }
           
            return View(newModel);
        }

        public async Task<IActionResult> Delete(int SysId)
        {
            var news = await _db.news.FirstOrDefaultAsync(x => x.SysId == SysId);
            if (news != null)
            {
                _db.Remove(news);
            }

            await _db.SaveChangesAsync();

            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> SaveAsync(NewsModel newModel)
        {
            var resultModel = new ResultModels();
            if (newModel.SysId > 0)
            {
                //Edit
                var news = await _db.news.FirstOrDefaultAsync(x => x.SysId == newModel.SysId);
                if (news != null)
                {
                    news.Content = newModel.Content;
                    news.TitleName = newModel.TitleName;
                    news.Status = newModel.Status;
                    news.Remark = newModel.Remark;
                    news.Modofier = User.Identity.Name;
                    news.UpdateTime = DateTime.Now;
                }
            }
            else
            {
                _db.news.Add(new News()
                {
                   Content = newModel.Content,
                   Creater = User.Identity.Name,
                   Modofier = User.Identity.Name,
                   CreateTime = DateTime.Now,
                   UpdateTime = DateTime.Now,
                   TitleName = newModel.TitleName,
                   Remark = newModel.Remark,
                   Status = newModel.Status
                });
            }

            var isSuccessful = await _db.SaveChangesAsync();
            if (isSuccessful > 0)
            {
                resultModel.IsSuccessful = true;
            }

            return Json(resultModel);
        }

        private IList<SelectListItem> GetStatus(string id)
        {
            IList<SelectListItem> lstItem = new List<SelectListItem>();
            lstItem.Add(new SelectListItem()
            {
                Text = "上线",
                Value = "0",
                Selected = "0".Equals(id)
            });
            lstItem.Add(new SelectListItem()
            {
                Text = "下线",
                Value = "1",
                Selected = "1".Equals(id)
            });

            return lstItem;
        }
    }
}
